var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
require('./common/doubleDate2.0')
var template = require('../tpl/template');
var minHeight = require('./common/minHeight');
var inputlimit = require('./common/input');
inputlimit.init();
minHeight.init();
var _nav = new nav();
var header = new _header();
var record = {
    init: function (medicalid) {
        this.bindevent();
        this.savext();
        this.savexueya();
        this.savesheng();
    },

    fetch: function (obj,cb) {
        var n = Object.assign({
            sign:"1",
            token:"str",
            name:"",
            basePressure:"",
            hightVoltage:"",
            height:"",
            weight:"",
            paramType:"",
            sugar:'',
            time:''
        },obj);
        util.fetch('api/chronic/saveOrupdate',n).then(function(res){
            alert('保存成功')  
            cb && cb()          
        })
    },
    savext: function (medicalid) {
        var _this = this;
        $('.js_save_xue').on('click', function(){
            var time = $('.js_rcord_xue_sel').data().text;
            var val = $('.js_xue_val').val();
            if (time && val){
                _this.fetch({
                    paramType:1,
                    name:"血糖",
                    sugar:val,
                    time:time,
                    
                },function(){
                    $('.js_rcord_xue_sel').data({text:""}).html('请选择测量时间');
                    $('.js_xue_val').val('')
                })
            }else {
                alert('请填写完整的信息')
            }
            
        })
    },
    savexueya: function (medicalid) {
        var _this = this;
        $('.js_save_xy').on('click', function(){
            var time = $('.js_record_ya_sel').data().text;
            var hig = $('.js_record_high').val();
            var low = $('.js_rcord_low').val();
            if(time && hig && low){
                _this.fetch({
                    name:"血糖",
                    hightVoltage:hig,
                    basePressure:low,
                    time:time,
                    paramType:2
                },function(){
                    $('.js_record_ya_sel').data({text:''}).html('请选择测量时间')
                    $('.js_record_high').val('')
                    $('.js_rcord_low').val('')
                })
            }else{
                alert('请填写完整的信息')
            }
        })
    },
    savesheng: function (medicalid) { // 保存身高体重
        var _this =this;
        $('.js_save_shengao').on('click', function(){
            var height = $('.js_record_height').val();
            var weight = $('.js_rcord_weight').val();
            if(height && weight){
                _this.fetch({
                    name:"身高体重",
                    paramType:3,
                    height:height,
                    weight:weight
                },function(){
                    $('.js_record_height').val('')
                    $('.js_rcord_weight').val('')
                })
            }else{
                alert('请填写完整的信息')
            }
        })
    },
    bindevent: function (medicalid) {
        // 控制输入位数
       
        $('.js_record_xue').on('click', function(){
            $('.js_record_xue_cont').toggleClass('hide')
        })
        $('.js_record_xue_cont').on('click','li',function() {
            $('.js_record_xue_cont').find('li').removeClass('on');
            $(this).addClass('on');
            $('.js_record_xue_cont').toggleClass('hide')

            $('.js_rcord_xue_sel').html($(this).html()).data({text:$(this).html()})
        })
        $('.js_record_ya').on('click', function(){
            $('.js_record_ya_cont').toggleClass('hide')
        })
        $('.js_record_ya_cont').on('click','li',function() {
            $('.js_record_ya_cont').find('li').removeClass('on');
            $(this).addClass('on');
            $('.js_record_ya_cont').toggleClass('hide')

            $('.js_record_ya_sel').html($(this).html()).data({text:$(this).html()})
        })
    }
}
record.init();
// 用药提醒
var tipuse = {
    tpl:'',
    time: function () {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + m + d ;
      },
    fetch: function (medicalid) {
        var _this=this;
        util.fetch('api/medicinesRemind/queryList?', {
            sign: "1"
        }).then(function (res) {
            console.log(res);

        })
        util.fetch('api/dazx/HIPMessageServerMbgl?action=app-chronic-disease-list&lx=1&startTime=20100101&method=HIPMessageServer',{
            endTime:_this.time(),
        }).then(function (res) {
            var s='';
            $('.js_suggest').removeClass('hide');
            for(var i=0;i<res.data.length;i++){
                s+=' <div class="n"><p class="a">'+res.data[i].jzsj+'</p> <p class="b">就诊机构：'+res.data[i].jgmc+'</p><p class="b">就诊时间：'+res.data[i].jzsj+'</p> <p class="b">就诊医生：'+res.data[i].jzys+'</p><p class="b">记录：'+res.data[i].jynr+'</p></div>'     
            }
            $('.js_suggest .miaoshu').html(s);
            
        }).fail(function(res){
            
        })
    },
    init: function (medicalid) {
        this.fetch();
        this.bindevent();
        // this.addremind();
        
    },
    addremind: function (medicalid) {
        util.fetch('api/medicinesRemind/saveOrUpdate',{
            sign:1,
            medicinesTime:'14:30pm' ,// 用药时间
            remindStartTime: '2018-09-12', //提醒开始时间
            remindEnd: '2018-09-11', // 提醒结束时间
            name:'肯德基' ,// 用药名称
            druUse:'手抓', // 用药方法，
            druTimes:'一日3次', // 用药次数
            singleDosage: '1', //单次用量
            unit: '天', //用药单位
            onOff: '1' // 0关闭，1打开 默认是0
        }).then(function (res){
            console.log(res)
        }).fail(function (res){
            console.log(res);
        })
    },
    deletd_remind: function (id) {
        var _this  = this;
        util.fetch('api/medicinesRemind/delteMedicinesRemind',{
            sign:'1',
            id:id
        }).then(function(res){
            _this.fetch();
            alert('删除成功');
        })
    },
    bindevent: function (medicalid) {
        var _this = this;
        $('.js_remind_yao_add').on('click', function () {
            $('.shadow1').show();
            _this.addremind()
        })
        $('body').on('click', '.js_remind_delete',function () {
            var id  = $(this).data().id;
            _this.deletd_remind(id);
        })
        $('.js_remind_js_cont').on('click','.js_close', function () {
            $(this).closest('.con').remove();
        })
    }
}
tipuse.init();
$('.js_date').kuiDate({
    className:'js_date',
    isDisabled: "0"  // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选 '2' 表示今日之后不可选
  });